<template>
  <div>
    <div class="provider-list">
      <div class="header">
        <div class="header-line1 flex-s">
          <el-button @click="onOperation({}, 'add')"
            ><el-icon style="margin-right: 2px"><Download /></el-icon
            >导出用户</el-button
          >
          <el-button @click="state.showUpload = true"
            ><el-icon style="margin-right: 2px"><Download /></el-icon
            >导入用户</el-button
          >
          <el-button
            v-if="!state.isSuccess"
            type="danger"
            @click="downloadFailFile"
            >下载失败数据</el-button
          >
          <el-button @click="downloadTemplate">下载模板</el-button>
        </div>
        <div class="header-line2 flex-s">
          <el-input
            v-model="state.params.neirong"
            placeholder="请输入内容"
            style="width: 300px"
            size="large"
          >
            <template #append>
              <el-button
                @click="GetList"
                style="min-width: 50px"
                icon="Search"
              />
            </template>
          </el-input>
          <el-select
            width="150"
            show-border
            class="ml20"
            v-model="state.params.huiyuan"
            size="large"
            placeholder="是否为会员"
            clearable
            @change="GetList"
          >
            <el-option
              v-for="item in state.memberships"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
          <el-select
            width="150"
            show-border
            class="ml20"
            v-model="state.params.qiye"
            size="large"
            placeholder="是否企业认证"
            clearable
            @change="GetList"
          >
            <el-option
              v-for="item in state.companies"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
          <el-select
            width="150"
            show-border
            class="ml20"
            v-model="state.params.zhuanjia"
            size="large"
            placeholder="是否专家认证"
            clearable
            @change="GetList"
          >
            <el-option
              v-for="item in state.excerpts"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
          <el-col :span="8">
            <el-date-picker
              type="daterange"
              range-separator="To"
              v-model="dateRange"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              size="large"
              class="ml20"
              prefix-icon="Calendar"
              value-format="YYYY-MM-DD"
              @change="changeTime"
            />
          </el-col>
        </div>
      </div>
      <div class="content content-table">
        <el-table
          ref="multipleTableRef"
          border
          :data="state.list"
          style="width: calc(100% - 20px)"
        >
          <el-table-column property="yhNo" label="ID" align="center" />
          <el-table-column
            property="nicheng"
            width="180"
            label="用户昵称"
            align="center"
          />
          <el-table-column
            property="shoujihao"
            label="手机号"
            width="150"
            align="center"
          />
          <el-table-column label="性别" width="80" align="center">
            <template #default="scope">
              <span>{{ scope.row?.xingbie?.code == 1 ? "男" : "女" }}</span>
            </template>
          </el-table-column>
          <el-table-column
            property="nianling"
            label="年龄"
            width="80"
            align="center"
          />
          <el-table-column label="是否为会员" width="100" align="center">
            <template #default="scope">
              <span>{{ scope.row?.huiyuan?.code == 1 ? "是" : "否" }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="会员类型"
            property="huiyuanleixing"
            width="130"
            align="center"
          >
          </el-table-column>
          <el-table-column label="企业信息" width="200" align="center">
            <template #default="scope"
              ><span>{{
                scope.row?.qiyerenzheng.code == 1 ? "已认证" : "未认证"
              }}</span></template
            >
          </el-table-column>
          <el-table-column label="专家信息" width="200" align="center">
            <template #default="scope"
              ><span>{{
                scope.row?.zhuanjiarenzheng.code == 1 ? "已认证" : "未认证"
              }}</span></template
            >
          </el-table-column>
          <el-table-column
            property="zuijindenglushijian"
            label="最近登录时间"
            width="200"
            align="center"
          />
          <el-table-column fixed="right" label="操作" width="80" align="center">
            <template #default="scope">
              <el-button
                style="min-width: auto"
                link
                type="primary"
                size="small"
                @click.prevent="onOperation(scope.row, 'detail')"
                >详情</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <h-page
          v-if="state.total"
          v-model:curr-page="state.params.current"
          :size="state.params.size"
          :total="state.total"
          @change="GetList"
        />
      </div>
    </div>
    <el-dialog
      class="list-dialog"
      show-close="false"
      width="35%"
      v-model="state.showUpload"
      center
      style="padding: 0 30px"
    >
      <p
        style="
          margin-top: -30px;
          margin-bottom: 30px;
          font-weight: bold;
          font-size: 20px;
          color: #333;
        "
      >
        导入用户
      </p>
      <el-form>
        <el-form-item label="上传附件：">
          <bjzOssXlsx
            v-if="state.showUpload"
            uploadType="user"
            @cancel="cancel"
            @success="success"
          ></bjzOssXlsx>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script setup>
import { reactive, watch, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { GetUserList } from "@/api/user";
import { downloadTFile } from "@/api/system";
import bjzOssXlsx from "@/components/bjz-oss-xlsx.vue";
const route = useRoute();
const router = useRouter();
const dateRange = ref("");
const state = reactive({
  params: {
    current: Number(route.query.page) || 1,
    size: Number(route.query.size) || 10,
    neirong: "",
    huiyuan: "",
    qiye: "",
    zhuanjia: "",
    kaishiriqi: "",
    jieshushijian: "",
  },
  memberships: [
    {
      label: "会员",
      value: "1",
    },
    {
      label: "非会员",
      value: "0",
    },
  ],
  companies: [
    {
      label: "已认证",
      value: "1",
    },
    {
      label: "未认证",
      value: "0",
    },
  ],
  excerpts: [
    {
      label: "已认证",
      value: "1",
    },
    {
      label: "未认证",
      value: "0",
    },
  ],
  list: [],
  total: 0,
  // 是否显示上传文件弹窗
  showUpload: false,
  // 上传的数据成功状态
  isSuccess: true,
  // 失败数据文件下载地址
  failFilePath: "",
  // 失败数据文件名
  failFileName: "",
});
const GetList = async () => {
  GetUserList(state.params).then((res) => {
    if (res.code == 4201) {
      state.list = res.data.records;
      state.total = res.data.total;
    }
  });
};
// 时间选择
const changeTime = (value) => {
  if (value) {
    state.params.kaishiriqi = value[0];
    state.params.jieshushijian = value[1];
  } else {
    state.params.kaishiriqi = "";
    state.params.jieshushijian = "";
  }
  GetList();
};
// 操作
const onOperation = (params, operation) => {
  if (operation === "detail") {
    router.push({
      path: `/yonghu/detail`,
      query: {
        id: params.id,
      },
    });
  }
};
// 下载文件方法
const downloadFile = (url, fileName) => {
  const x = new window.XMLHttpRequest();
  x.open("GET", url, true);
  x.responseType = "blob";
  x.onload = () => {
    const url = window.URL.createObjectURL(x.response);
    const a = document.createElement("a");
    console.log(url);
    a.href = url;
    a.download = fileName;
    a.click();
  };
  x.send();
};
// 下载模板
const downloadTemplate = () => {
  downloadTFile({ type: "yonghu" }).then((res) => {
    if (res.code == 4201) {
      downloadFile(res.data.filePath, res.data.fileName);
    }
  });
};
// 下载失败文件模板
const downloadFailFile = () => {
  downloadFile(state.failFilePath, state.failFileName);
};
// 取消上传
const cancel = (val) => {
  state.showUpload = val;
};
// 文件上传完成
const success = (data) => {
  state.isSuccess = data.result;
  if (!data.result) {
    baseMsg("部分数据失败,请下载失败文件再重新上传", false);
    state.failFilePath = data.resultFilePath;
    state.failFileName = data.fileName;
  } else {
    baseMsg("上传成功");
  }
  GetList();
  state.showUpload = false;
};
watch(
  () => route.query,
  () => {
    state.params.current = Number(route.query.page) || 1;
    GetList();
  },
  { deep: true }
);
GetList();
</script>
<style lang="scss" scoped>
.header {
  border-bottom: 10px solid #f2f2f2;
  border-radius: 10px 10px 0 0;
}
.header-line1 {
  padding: 20px 10px;
  border-bottom: 1px solid #ccc;
  border-radius: 10px 10px 0 0;
}
.header-line2 {
  padding: 20px 10px;
}
.content {
  border: none !important;
  margin: 20px 10px;
  background-color: transparent !important;
}
.content-table {
  padding-bottom: 30px;
}
</style>
<style lang="scss">
.content-table .el-table__header th {
  background-color: #f2f9fc !important;
}
.note-button-group {
  .el-form-item__content {
    justify-content: center;
  }
}
</style>
